package com.salary.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.salary.dao.QiandaoMapper;
import com.salary.dao.QingjiaMapper;
import com.salary.dao.WorkMapper;
import com.salary.entity.Qiandao;
import com.salary.entity.Qingjia;
import com.salary.entity.Work;
import com.salary.entity.view.QiandaoView;
import com.salary.entity.view.QingjiaView;
import com.salary.qpage.QPage;
import com.salary.service.IQiandaoService;
import com.salary.service.IQingjiaService;
import com.salary.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalTime;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 签到接口实现层
 */
@Service
public class QiandaoServiceImpl extends ServiceImpl<QiandaoMapper, Qiandao> implements IQiandaoService {

    @Autowired
    private WorkMapper workMapper;

    /**
     * 列表分页
     * @param qPage
     * @param obj
     * @return
     */
    @Override
    public IPage getList(QPage qPage, QiandaoView obj) {
        IPage iPage = new Page(qPage.getOffset(), qPage.getLimit());
        List<QingjiaView> mapList = baseMapper.list(iPage,obj);
        return iPage.setRecords(mapList);
    }

    /**
     * 新增
     * @param obj
     * @return
     */
    @Override
    public Result insert(Qiandao obj) {
        //查看当天用户是否已经签到过
        QiandaoView qiandao=this.baseMapper.getQiandao(obj.getEmployeeId());
        if(qiandao!=null){
            return Result.reError("你当天已经签到过了，无须重新签到");
        }
        obj.setQddate(new Date());
        this.baseMapper.insert(obj);
        Work work=workMapper.getDay(obj.getEmployeeId());
        if(work==null){
            work=new Work();
            work.setEmployeeId(obj.getEmployeeId());
            //if(obj.getQddate())
            // 创建一个 Calendar 对象
            Calendar calendar = Calendar.getInstance();
            // 将当前时间设置到 Calendar 对象中
            calendar.setTime(new Date());
            // 比较当前时间的小时是否大于9点
            if (calendar.get(Calendar.HOUR_OF_DAY) > 9) {
               work.setCdnum(1);
            }else{
                work.setCdnum(0);
            }
            work.setCqnum(1);
            work.setTdate(new Date());
            work.setCtime(new Date());
            this.workMapper.insert(work);
        }else{
            Calendar calendar = Calendar.getInstance();
            // 将当前时间设置到 Calendar 对象中
            calendar.setTime(new Date());
            // 比较当前时间的小时是否大于9点
            if (calendar.get(Calendar.HOUR_OF_DAY) > 9) {
                work.setCdnum(1);
            }
            work.setCqnum(work.getCqnum()+1);
            work.setCdnum(work.getCdnum()+1);
            this.workMapper.updateById(work);
        }
        return Result.reOk("签到成功");
    }

    /**
     * 修改
     * @param obj
     * @return
     */
    @Override
    public Result updates(Qiandao obj) {
        QiandaoView qiandao=this.baseMapper.getQiandao(obj.getEmployeeId());
        if(qiandao==null){
            return Result.reError("请当天先签到，再来签退");
        }
        //查看当天用户是否已经签到过
        QiandaoView qiantui=this.baseMapper.getQiantui(obj.getEmployeeId());
        if(qiantui!=null){
            return Result.reError("当天你已经签退过了，无须重复签退");
        }
        qiandao.setId(qiandao.getId());
        qiandao.setQtdate(new Date());
        int isOk=this.baseMapper.updateById(qiandao);
        if(isOk==1){
            Work work=workMapper.getDay(obj.getEmployeeId());
            Calendar calendar = Calendar.getInstance();
            // 将当前时间设置到 Calendar 对象中
            calendar.setTime(new Date());
            // 比较当前时间的小时是否大于9点
            if (calendar.get(Calendar.HOUR_OF_DAY) < 17) {
                work.setZtnum(work.getZtnum()+1);
            }
            this.workMapper.updateById(work);
            return Result.reOk("签退成功");
        }
        return Result.reError("签退失败");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public Result deleteId(Integer id) {
        int isOk=this.baseMapper.deleteById(id);
        if(isOk==1){
            return Result.reOk("删除成功");
        }
        return Result.reError("删除失败");
    }



}
